from socket import *
from select import select
IP="127.0.0.1"
PORT=5555
ADDR=(IP,PORT)

s=socket()
s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
s.bind(ADDR)
rlist=[s]
wlist=[]
xlist=[]
s.listen(15)
while True:
    rs,ws,xs=select(rlist,wlist,xlist)
    # 遍历就绪的IO
    for item in rs:
        # 如果是s就接收连接
        if item is s:
            print("waiting for connection ....")
            conn,addr=s.accept()
            print("connect from",addr)
            rlist.append(conn)
        else:
            data=item.recv(1024).decode()
            if not data:
                rlist.remove(item)
                continue
            print(data)
            wlist.append(item)

    for item in ws:
        item.send("welcome".encode())
        wlist.remove(item)

    for item in xs:
        pass